﻿using System.IO;
using System.Windows.Forms;

namespace Editor
{
    public class SceneManager
    {
        private string _filePath;


        public string Scene { get; private set; }
        private bool IsOpen { get { return _filePath != string.Empty; } }

        public SceneManager()
        {
            _filePath = string.Empty;
            Scene = string.Empty;
            if (Properties.Settings.Default.recentlyFiles == null)
            {
                Properties.Settings.Default.recentlyFiles = new System.Collections.Specialized.StringCollection();
                Properties.Settings.Default.Save();
            }
        }
        public SceneManager(string filePath)
        {
            Scene = File.ReadAllText(filePath);
            _filePath = filePath;
            AddRecentlyFiles();
        }
        private void AddRecentlyFiles()
        {
            if (Properties.Settings.Default.recentlyFiles.Count > 10)
                Properties.Settings.Default.recentlyFiles.RemoveAt(10);
            if (!Properties.Settings.Default.recentlyFiles.Contains(_filePath))
                Properties.Settings.Default.recentlyFiles.Insert(0, _filePath);
            Properties.Settings.Default.Save();
        }
        public bool Equals(string newScene)
        {
            return Scene == newScene;
        }
        public void Save(string scene)
        {
            if (IsOpen)
            {
                File.WriteAllText(_filePath, scene);
                Scene = scene;
                AddRecentlyFiles();
            }
            else
                SaveAs(scene);

        }
        public void SaveAs(string scene)
        {
            var dlg = new SaveFileDialog();
            if (dlg.ShowDialog() == DialogResult.OK)
            {
                File.WriteAllText(dlg.FileName, scene);
                Scene = scene;
                _filePath = dlg.FileName;
                AddRecentlyFiles();
            }
        }
    }
}
